package cn.edu.sgu.www.mhxysy.function.computer.attribute.impl;

import cn.edu.sgu.www.mhxysy.entity.equipment.Equipment;
import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentTexiao;
import cn.edu.sgu.www.mhxysy.entity.except.AdvancedProperty;
import cn.edu.sgu.www.mhxysy.entity.role.RoleAccount;
import cn.edu.sgu.www.mhxysy.entity.xingyin.Xingyin;
import cn.edu.sgu.www.mhxysy.enums.WearStatus;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.function.computer.attribute.AttributeComputer;
import cn.edu.sgu.www.mhxysy.mapper.baoshi.BaoshiMapper;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentMapper;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentTexiaoMapper;
import cn.edu.sgu.www.mhxysy.mapper.except.AdvancedPropertyMapper;
import cn.edu.sgu.www.mhxysy.mapper.fabao.FabaoMapper;
import cn.edu.sgu.www.mhxysy.mapper.potency.PotencyFruitPatternMapper;
import cn.edu.sgu.www.mhxysy.mapper.role.RoleAccountMapper;
import cn.edu.sgu.www.mhxysy.mapper.school.SchoolSkillMapper;
import cn.edu.sgu.www.mhxysy.mapper.xingyin.XingyinAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.xingyin.XingyinMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.util.CollectionUtils;
import cn.edu.sgu.www.mhxysy.vo.baoshi.BaoshiVO;
import cn.edu.sgu.www.mhxysy.vo.except.AttributeVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色高级属性计算器
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Component
public class AdvancedPropertyComputer implements AttributeComputer {

    private static List<AdvancedProperty> list;
    private static Map<String, Integer> advancedProperty = null;

    private final FabaoMapper fabaoMapper;
    private final BaoshiMapper baoshiMapper;
    private final XingyinMapper xingyinMapper;
    private final EquipmentMapper equipmentMapper;
    private final RoleAccountMapper roleAccountMapper;
    private final SchoolSkillMapper schoolSkillMapper;
    private final EquipmentTexiaoMapper equipmentTexiaoMapper;
    private final AdvancedPropertyMapper advancedPropertyMapper;
    private final XingyinAttributeMapper xingyinAttributeMapper;
    private final PotencyFruitPatternMapper potencyFruitPatternMapper;

    @Autowired
    public AdvancedPropertyComputer(
            FabaoMapper fabaoMapper,
            BaoshiMapper baoshiMapper,
            XingyinMapper xingyinMapper,
            EquipmentMapper equipmentMapper,
            RoleAccountMapper roleAccountMapper,
            SchoolSkillMapper schoolSkillMapper,
            EquipmentTexiaoMapper equipmentTexiaoMapper,
            AdvancedPropertyMapper advancedPropertyMapper,
            XingyinAttributeMapper xingyinAttributeMapper,
            PotencyFruitPatternMapper potencyFruitPatternMapper) {
        this.fabaoMapper = fabaoMapper;
        this.baoshiMapper = baoshiMapper;
        this.xingyinMapper = xingyinMapper;
        this.equipmentMapper = equipmentMapper;
        this.roleAccountMapper = roleAccountMapper;
        this.schoolSkillMapper = schoolSkillMapper;
        this.equipmentTexiaoMapper = equipmentTexiaoMapper;
        this.advancedPropertyMapper = advancedPropertyMapper;
        this.xingyinAttributeMapper = xingyinAttributeMapper;
        this.potencyFruitPatternMapper = potencyFruitPatternMapper;
    }

    @Override
    public Map<String, Integer> apply(String roleId) {
        RoleAccount roleAccount = roleAccountMapper.selectById(roleId);

        if (roleAccount == null) {
            throw new GlobalException(ResponseCode.CONFLICT, "操作失败，角色不存在！");
        }

        // 查询所有角色高级属性类型
        list = advancedPropertyMapper.selectList(null);

        if (advancedProperty == null) {
            // 初始化resultMap
            advancedProperty = new HashMap<>(list.size());
        }

        for (AdvancedProperty advancedProperty : list) {
            AdvancedPropertyComputer.advancedProperty.put(advancedProperty.getName(), 0);
        }

        /*
         * 查询高级属性并赋值
         */
        // 1、查询角色穿戴的装备
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("role_id", roleId);
        queryWrapper.eq("wear_status", WearStatus.YCD.getValue());

        List<Equipment> equipmentList = equipmentMapper.selectList(queryWrapper);

        if(CollectionUtils.isNotEmpty(equipmentList)) {
            for (Equipment equipment : equipmentList) {
                // 装备特效增加的属性
                EquipmentTexiao equipmentTexiao = equipmentTexiaoMapper.selectById(equipment.getTexiaoId());

                addProperty(equipmentTexiao.getName(), equipmentTexiao.getPropertyValue());

                // 装备宝石增加的属性
                List<BaoshiVO> attributes = baoshiMapper.selectAttributes(equipment.getId());

                if (CollectionUtils.isNotEmpty(attributes)) {
                    for (BaoshiVO attribute : attributes) {
                        String propertyName = attribute.getPropertyName();
                        Integer propertyValue = attribute.getGrade() * attribute.getPropertyValue();

                        addProperty(propertyName, propertyValue);
                    }
                }
            }
        }

        // 2、查询角色穿戴的星印
        Map<String, Object> columnMap = new HashMap<>();

        columnMap.put("role_id", roleId);
        columnMap.put("wear_status", WearStatus.YCD.getValue());

        List<Xingyin> xingyins = xingyinMapper.selectByMap(columnMap);

        if (CollectionUtils.isNotEmpty(xingyins)) {
            for (Xingyin xingyin : xingyins) {
                List<AttributeVO> properties = xingyinAttributeMapper.selectAttributes(xingyin.getId());

                addProperty(properties);
            }
        }

        // 3、查询角色的法宝
        List<AttributeVO> properties = fabaoMapper.selectProperty(roleId);

        addProperty(properties);

        // 4、查询角色的潜能果
        properties = potencyFruitPatternMapper.selectAdvancedProperty(roleId);

        addProperty(properties);

        // 5、查询角色的门派技能
        properties = schoolSkillMapper.selectAdvancedProperty(roleId);

        addProperty(properties);

        // 6、todo:查询角色的经脉

        return advancedProperty;
    }

    /**
     * 增加属性
     * @param properties 属性列表
     */
    private void addProperty(List<AttributeVO> properties) {
        if (CollectionUtils.isNotEmpty(properties)) {
            for (AttributeVO property : properties) {
                String propertyName = property.getPropertyName();
                Integer propertyValue = property.getPropertyValue();

                addProperty(propertyName, propertyValue);
            }
        }
    }

    /**
     * 增加属性
     * @param propertyName 属性名
     * @param propertyValue 属性值
     */
    private void addProperty(String propertyName, Integer propertyValue) {
        for (AdvancedProperty advancedProperty : list) {
            String name = advancedProperty.getName();
            String note = advancedProperty.getNote();

            if (propertyName.contains(note)) {
                Integer value = AdvancedPropertyComputer.advancedProperty.get(name);

                AdvancedPropertyComputer.advancedProperty.put(name, value + propertyValue);
                break;
            }
        }
    }

}